package edu.thu.studio.ormdesigner.outline.command;

import java.util.List;

import edu.thu.studio.ormdesigner.common.AbstractCommand;
import edu.thu.studio.ormdesigner.model.ORMModel;
import edu.thu.studio.ormdesigner.model.diagram.Diagram;
import edu.thu.studio.ormdesigner.model.diagram.node.Node;
import edu.thu.studio.ormdesigner.model.orm.view.View;
import edu.thu.studio.ormdesigner.util.Utils;

public class UpdateViewCommand extends AbstractCommand {
    
    private View view;
    
    public UpdateViewCommand(View view) {
        this.view = view;
    }
    
    @Override
    protected void doExecute() {
        this.view.refresh();
        this.refreshNodesByView(view);
    }
    
    @Override
    protected void doUndo() {
        this.view.refresh();
        this.refreshNodesByView(view);
    }
    
    private void refreshNodesByView(View view) {
        // 需要更新所有的package 而非当前package
        ORMModel ormModel = Utils.getORMModel();
        for (Diagram diagram : ormModel.getDiagrams()) {
            List<Node> list = diagram.getNodes().filterByRefId(view.getId());
            for (Node node : list) {
                node.refresh();
            }
        }
    }
    
}
